package com.atmaowei.service.impl;

import com.atmaowei.controller.request.BaseRequest;
import com.atmaowei.controller.request.CourseRecordPageRequest;
import com.atmaowei.entity.CourseRecord;
import com.atmaowei.entity.Sct;
import com.atmaowei.mapper.SelectCourseMapper;
import com.atmaowei.service.SelectCourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelectCourseServiceImpl implements SelectCourseService {

    @Autowired
    private SelectCourseMapper selectCourseMapper;

    @Override
    public String getCpnameByCpno(String cpno) {
        return selectCourseMapper.getCpnameByCpno(cpno);
    }

    @Override
    public String getTnameByTno(String tno) {
        return selectCourseMapper.getTnameByTno(tno);
    }

    @Override
    public String getCpnoByCno(String cno) {
        return selectCourseMapper.getCpnoByCno(cno);
    }

    @Override
    public String getCcredit(String cno) {
        return selectCourseMapper.getCcredit(cno);
    }

    @Override
    public PageInfo<CourseRecord> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<CourseRecord> courses = selectCourseMapper.page(baseRequest);
        for (CourseRecord course : courses) {
            if (course.getCpno().equals("无")) {
                course.setCpname("无");
            } else {
                course.setCpname(getCpnameByCpno(course.getCpno()));
            }
        }
        PageInfo<CourseRecord> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }

    @Override
    public PageInfo<CourseRecord> recordPage(CourseRecordPageRequest courseRecordPageRequest) {
        PageHelper.startPage(courseRecordPageRequest.getPageNum(), courseRecordPageRequest.getPageSize());
        List<CourseRecord> courses = selectCourseMapper.recordPage(courseRecordPageRequest);
        for (CourseRecord course : courses) {
            course.setCname(getCpnameByCpno(course.getCno()));
            course.setTname(getTnameByTno(course.getTno()));
            course.setCcredit(getCcredit(course.getCno()));
            if (getCpnoByCno(course.getCno()).equals("无")) {
                course.setCpno("无");
                course.setCpname("无");
            } else {
                course.setCpno(getCpnoByCno(course.getCno()));
                course.setCpname(getCpnameByCpno(getCpnoByCno(course.getCno())));
            }
        }
        PageInfo<CourseRecord> pageInfo = new PageInfo<>(courses);
        return pageInfo;
    }

    @Override
    public void addSct(String sno, String cno, String tno) {
        selectCourseMapper.addSct(sno,cno,tno);
    }

    @Override
    public Sct querySct(String sno, String cno, String tno) {
        return selectCourseMapper.querySct(sno, cno, tno);
    }

    @Override
    public void cancelSct(String sno, String cno, String tno) {
        selectCourseMapper.cancelSct(sno,cno,tno);
    }

    @Override
    public void updatePassword(String sno, String password) {
        selectCourseMapper.updatePassword(sno, password);
    }
}
